#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <QInputDialog>
#include <QTextBrowser>
#include <QTableWidget>
#include <QComboBox>
#include <QPen>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QList>
#include <QString>
#include <QMimeData>
#include "EQSignal.h"
#include "qcustomplot.h"

namespace Ui {
class MainWindow;
}

class EQTableWidget : public QTableWidget
{
    Q_OBJECT
public:
    EQTableWidget(QWidget *parent=0) : QTableWidget(parent) {}

    double *getColumnData(int col);
    double *getRowData(int row);

signals:
    void rowNumberChanged(int);

public slots:
    void setColumnNumber(int nc) {this->setColumnCount(nc);}
    void setRowNumber(int nr) {this->setRowCount(nr);}

protected:
    void keyPressEvent(QKeyEvent *e);

};

class SpectraDefineWidget : public QWidget
{
    Q_OBJECT
public:
    SpectraDefineWidget(QWidget *parent=0);

    QVBoxLayout *wl;
    QHBoxLayout *bl;

    QPushButton *readButton;
    QPushButton *applyButton;

    QLabel *label_np;
    QSpinBox *spinBox_np;

    EQTableWidget *dataTable;

};

class PeriodsDefineWidget : public SpectraDefineWidget
{
    Q_OBJECT
public:
    PeriodsDefineWidget(QWidget *parent=0);

    double *getPeriods() {return dataTable->getColumnData(0);}

};

class SPTDefineWidget : public SpectraDefineWidget
{
    Q_OBJECT
public:
    SPTDefineWidget(QWidget *parent=0);

    double *getPeriods() {return dataTable->getColumnData(0);}
    double *getSPT() {return dataTable->getColumnData(1);}

};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void readtxt(const char *filename, double dt);
    void readtxt(QString filename, double dt);
    void readnga(const char *filename);
    void readnga(QString filename);

    static QPen autoPen(int i);
    static QColor reverseColor(QColor c);

protected:
    void dragEnterEvent(QDragEnterEvent *event) {
        if (event->mimeData()->hasFormat("text/uri-list"))
            event->acceptProposedAction();
    }

    void dropEvent(QDropEvent *event);

public slots:

    void showDriftMsg();
    void confirm();
    void saveTH();
    void saveSP();
    void setupSP();

    void plotTH(bool changeTab=true);
    void plotRES();
    void plotEnergy();
    void plotHyst();
    void plotSPA();
    void plotSP();
    void plotFFT();
    void plotPSD();
    void plotSPT();
    void plotSPAi(bool preFit=false);

    void SPXScaleChanged();

    void fillSPTable(int k=0);
    void fillTHTable();

    void pdw_applyButton_clicked();
    void sptw_applyButton_clicked();

    void fillRESTable();

private slots:

    void on_Norm_clicked();

    void on_Reload_clicked();

    void on_Filt_clicked();

    void on_Detrend_clicked();

    void on_Align_clicked();

    void on_Adjust_clicked();

    void on_SetTrimEdge_clicked();

    void on_Trim_clicked();

    void on_CalcFFT_clicked();

    void on_CalcPSD_clicked();

    void on_CalcSPA_clicked();

    void on_CalcSP_clicked();

    void on_DR_textChanged(const QString &arg1);

    void on_actionOpen_triggered();

    void on_actionSaveData_triggered();

    void on_actionSaveFig_triggered();

    void on_actionSaveAcc_triggered();

    void on_GenSPT_clicked();

    void on_actionViewData_triggered();

    void on_DM_activated(int index);

    void on_TSPT_activated(int index);

    void on_SPFit_clicked();
    
    void on_Paras_currentChanged(int index);

    void on_SPComPare_clicked();

    void on_CalcRes_clicked();

private:
    void setupConnections();
	void initViewTH();
    void initViewRES();
    void initViewHyst();
	void initViewSPA();
    void initViewSP();
    void initViewFFT();
    void initViewEnergy();
    void initTable();

    void clearView(QCustomPlot *qplot);

    void readConfig();
    void writeConfig();

    Ui::MainWindow *ui;

    EQSignal *eqs;
    EQSignal *eqs0;

    QString workDir;
    QString eqsName;

    bool saveAccWithTime;
    bool normOnRead;

    EQTableWidget *dataTableTH;
    EQTableWidget *dataTableRES;
    EQTableWidget *dataTableSP;

    PeriodsDefineWidget *pdw;
    SPTDefineWidget *sptw;

    QWidget *wth;
    QWidget *wres;
    QWidget *wsp;

    QComboBox *dr;

    void ViewTHData();
    void ViewSPData();

};

#endif // MAINWINDOW_H
